package com.hlk.user.controller;

import com.hlk.constant.Constant;
import com.hlk.controller.user.AppUserMngControllerApi;
import com.hlk.enums.ResponseStatusEnum;
import com.hlk.exception.HuaException;
import com.hlk.pojo.AppUser;
import com.hlk.pojo.vo.PublisherVO;
import com.hlk.result.Result;
import com.hlk.user.service.AppUserMngService;
import com.hlk.user.service.UserService;
import com.hlk.utils.JsonUtils;
import com.hlk.utils.PageResult;
import com.hlk.utils.redis.RedisKeys;
import com.hlk.utils.redis.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author hlk
 * @version 1.0
 * @date 2020-12-29 14:28
 */
@RestController
public class AppUserMngController implements AppUserMngControllerApi {
    @Autowired
    private AppUserMngService appUserMngService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private UserService userService;


    @Override
    public Result queryAll(String nickname, Integer status, Date startDate, Date endDate, Integer page, Integer pageSize) {
        if (page == null) {
            page = Constant.PAGE;
        }

        if (pageSize == null) {
            pageSize = Constant.PAGESIZE;
        }

        PageResult result = appUserMngService.selectAllUserList(nickname, status, startDate, endDate, page, pageSize);

        return Result.ok(result);
    }

    @Override
    public Result userDetail(String userId) {
        if (StringUtils.isBlank(userId)) {
            throw new HuaException(ResponseStatusEnum.USER_NOT_EXIST_ERROR);
        }
        AppUser appUser = appUserMngService.selectUserByUserId(userId);
        return Result.ok(appUser);
    }

    @Override
    public Result freezeUserOrNot(String userId, Integer doStatus) {
        if (StringUtils.isBlank(userId)) {
            throw new HuaException(ResponseStatusEnum.USER_NOT_EXIST_ERROR);
        }

        appUserMngService.freezeUserOrNot(userId, doStatus);

        // 删除缓存中的用户信息
        redisUtils.del(RedisKeys.getUserInfoKey(userId));
        return Result.ok();
    }

    @Override
    public Result queryAll(String userIds) {
        if (StringUtils.isBlank(userIds)) {
            return Result.errorCustom(ResponseStatusEnum.USER_NOT_EXIST_ERROR);
        }

        List<String> userIdList = JsonUtils.jsonToList(userIds, String.class);
//        System.out.println(userIdList);

        List<PublisherVO> userList = userService.getUserList(userIdList);
        return Result.ok(userList);
    }
}
